/**
 * Package name:indi.yangshenhui.crawlers.blind
 * File name:Blind.java
 * Date:2016年7月23日-上午6:52:49
 * feiniu.com Inc.Copyright (c) 2013-2015 All Rights Reserved.
 *
 */
package indi.yangshenhui.crawlers.blind;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.wanghaomiao.seimi.annotation.Crawler;
import cn.wanghaomiao.seimi.def.BaseSeimiCrawler;
import cn.wanghaomiao.seimi.struct.Response;
import cn.wanghaomiao.xpath.model.JXDocument;
import indi.yangshenhui.tool.DateUtil;
import indi.yangshenhui.tool.dao.DBHelper;

/**
 * @ClassName Blind
 * @Description
 * @date 2016年7月23日 上午6:52:49
 * @author shenhui
 * @version 1.0.0
 *
 */
@Crawler(name = "Blind")
public class Blind extends BaseSeimiCrawler {
	public String tiebaBaidu = "http://tieba.baidu.com";

	@Override
	public void start(Response response) {
		JXDocument doc = response.document();
		try {
			List<Object> urls = doc.sel("//a[@class='j_th_tit ']/@href");
			List<Object> titles = doc.sel("//a[@class='j_th_tit ']/@title");
			String sql = "INSERT INTO post(url,title,ctime,mtime) VALUES(?,?,?,?)";
			for (int i = 0; i < urls.size(); i++) {
				Object object = urls.get(i);
				DBHelper.executeNonQuery(sql, tiebaBaidu+object.toString(), titles.get(i).toString() ,DateUtil.format(new Date()), DateUtil.format(new Date()));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public String[] startUrls() {
		int pn = 1062450;
		int ps = 50;
		int totalCount = (pn % ps != 0) ? (pn / ps + 1) : (pn / ps);
		String baseUrl = "http://tieba.baidu.com/f?kw=%E7%9B%B8%E4%BA%B2&ie=utf-8&pn=";
		List<String> urlList = new ArrayList<String>();
		String sql = "INSERT INTO url(name,ctime,mtime) VALUES(?,?,?)";
		String url = "";
		for (int i = 1; i <= totalCount; i++) {
			url = baseUrl + (ps * i) + "";
			urlList.add(url);
			DBHelper.executeNonQuery(sql, url, DateUtil.format(new Date()), DateUtil.format(new Date()));
			/*if (i == 1) {
				break;
			}*/
		}
		return urlList.toArray(new String[urlList.size()]);
	}

}
